package com.csl.jsonpreference.ui;

import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.csl.jsonpreference.R
import com.csl.jsonpreference.bean.*
import com.csl.jsonpreference.configdes.JsonArrayPreference
import com.csl.jsonpreference.configdes.JsonBasePreference
import com.csl.jsonpreference.configdes.JsonConfigIns
import com.csl.jsonpreference.configdes.JsonObjectPreference
import com.csl.jsonpreference.databinding.ItemPreferenceBinding
import com.csl.jsonpreference.util.CLog
import com.google.gson.Gson
import org.json.JSONObject
import java.util.*


/**
 * 作者：LIUCHANGSONG on 2020/1/7 10:51
 * 邮箱：cs_lius@126.com
 */
abstract class JsonPreferenceAdapter(
) : RecyclerView.Adapter<JsonPreferenceAdapter.JsonItemHolder>() {

    abstract fun onPreferenceClick(index: Int, data:JSONObject, config: JsonConfigIns)

    var datas = ArrayList<JsonConfigIns>()
    var jsonDatas: JSONObject = JSONObject()

    fun addData(data: JsonConfigIns) {
        if (!datas.contains(data)) {
            datas.add(data)
        }
    }
    fun resetData(jsonData: JSONObject, jsonConfig: ArrayList<JsonConfigIns>?) {
        datas.clear()
        jsonConfig?.let {
            datas.addAll(jsonConfig)
        }
        jsonDatas = jsonData
        notifyDataSetChanged()
    }

    fun clearData() {
        datas.clear()
        jsonDatas = JSONObject()
        notifyDataSetChanged()
    }


    inner class JsonItemHolder(var viewBinding: ItemPreferenceBinding) :
        ViewHolder(viewBinding.root) {
        var nowIndex = -1

        init {
            viewBinding.root.setOnClickListener {
                var data = datas[nowIndex]
                onPreferenceClick(nowIndex, jsonDatas, data)
            }
        }

        fun setData(index: Int, config: JsonConfigIns) {
            var title = if (TextUtils.isEmpty(config.title)) {
                config.key
            } else {
                config.title
            }
            viewBinding.jsonTvTitle.text = "$title"
            if(TextUtils.isEmpty(config.describe)) {
                viewBinding.jsonTvDes.visibility = View.GONE
            } else {
                viewBinding.jsonTvDes.visibility = View.VISIBLE
                viewBinding.jsonTvDes.text = config.describe
            }

            when (config.configType) {
                ConfigTYPE.BOOLEAN -> {
                    viewBinding.jsonTvValue.visibility = View.GONE
                    viewBinding.jsonVColor.visibility = View.GONE
                    viewBinding.jsonCbValue.visibility = View.VISIBLE
                    viewBinding.jsonCbValue.isChecked = jsonDatas.optBoolean(
                        config.key,
                        false
                    )
                }

                ConfigTYPE.COLOR -> {
                    viewBinding.jsonCbValue.visibility = View.GONE
                    viewBinding.jsonVColor.visibility = View.VISIBLE
                    viewBinding.jsonTvValue.visibility = View.GONE
                    viewBinding.jsonVColor.setBackgroundColor(
                        jsonDatas.optInt(
                            config.key,
                            viewBinding.jsonTvDes.context.resources.getColor(R.color.colorPrimary)
                        )
                    )
                }
//                ConfigTYPE.ARRAY_BOOLEAN,
//                ConfigTYPE.ARRAY_OBJECT,
//                ConfigTYPE.ARRAY_FLOAT,
//                ConfigTYPE.ARRAY_NUMBER,
//                ConfigTYPE.ARRAY_TXT -> {
//                    viewBinding.jsonTvValue.visibility = View.VISIBLE
//                    viewBinding.jsonTvValue.text = jsonDatas.optString(
//                        config.key,
//                        ""
//                    )
//                    viewBinding.jsonCbValue.visibility = View.GONE
//                }
                ConfigTYPE.ENUM -> {
                    viewBinding.jsonCbValue.visibility = View.GONE
                    viewBinding.jsonVColor.visibility = View.GONE
                    viewBinding.jsonTvValue.visibility = View.VISIBLE
                    if(config is JsonBasePreference) {
                        CLog.d("enum", "枚举类型:${config.classType}")

                        viewBinding.jsonTvValue.text = jsonDatas.optString(config.key, "")
                    } else {
                        viewBinding.jsonTvValue.text = jsonDatas.optString(config.key, "")
                    }
//                    viewBinding.jsonTvValue.text = jsonDatas.op
                }
                ConfigTYPE.OBJECT, ConfigTYPE.ARRAY_OBJECT-> {
                    viewBinding.jsonCbValue.visibility = View.GONE
                    viewBinding.jsonVColor.visibility = View.GONE
                    viewBinding.jsonTvValue.visibility = View.VISIBLE
                    var valueStr = config.getParseValueStr()

                    if(TextUtils.isEmpty(valueStr)) {
                        viewBinding.jsonTvValue.visibility = View.GONE
                    } else {
                        viewBinding.jsonTvValue.visibility = View.VISIBLE
                        viewBinding.jsonTvValue.text = valueStr
                    }
                }
                else -> {
                    viewBinding.jsonCbValue.visibility = View.GONE
                    viewBinding.jsonVColor.visibility = View.GONE
                    viewBinding.jsonTvValue.visibility = View.VISIBLE
                    var value = jsonDatas.optString(config.key, "")
                    if(TextUtils.isEmpty(value)) {
                        viewBinding.jsonTvValue.visibility = View.GONE
                    } else {
                        viewBinding.jsonTvValue.visibility = View.VISIBLE
                        viewBinding.jsonTvValue.text = value
                    }
                }
            }
        }



    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): JsonItemHolder {
        var inflater = LayoutInflater.from(parent.context)
        return JsonItemHolder(ItemPreferenceBinding.inflate(inflater, parent, false))
    }

    override fun onBindViewHolder(holder: JsonItemHolder, position: Int) {
        var config = datas[position]
        holder.nowIndex = position
        holder.setData(position, config)
    }

    override fun getItemCount(): Int {
        return datas.size
    }

    override fun getItemViewType(position: Int): Int {
//        return datas[position].type.ordinal
        return 0
    }


}
